package com.humanresource.action.clerkrecord;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.humanresource.constant.Constant;
import com.humanresource.domain.po.clerkrecord.Clerkbaseinfo;
import com.humanresource.service.clerkrecord.ClerkBaseInfoService;

@Controller
@RequestMapping("/PersonManSystemHtml")
public class PhotoUploadController {
	@Autowired
	private ClerkBaseInfoService clerkBaseInfoService;

	/**
	 * 
	 * @param uploadfile
	 * @param session
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/head/upload/{clerkname}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String updateHeadImage(
			@RequestParam("uploadfile") MultipartFile uploadfile,
			@PathVariable(value = "clerkname") String clerkname,
			HttpServletRequest request) throws IOException {

		String headname = uploadfile.getOriginalFilename();
		String path = request.getServletContext().getRealPath(File.separator)
				+ "upload" + File.separatorChar + clerkname
				+ File.separatorChar + headname;
		// System.out.println(path);
		// 上传文件写磁盘
		FileUtils.writeByteArrayToFile(new File(path), uploadfile.getBytes());

		String sqlheadpath = Constant.PATH + File.separatorChar + "upload"
				+ File.separatorChar + clerkname + File.separatorChar
				+ headname;
		// System.out.println(sqlheadpath);
		Clerkbaseinfo clerkbaseinfo = this.clerkBaseInfoService
				.findByName(clerkname);
		// 保存用户头像的存放路径
		clerkbaseinfo.setClerkphotopath(sqlheadpath);
		this.clerkBaseInfoService.update(clerkbaseinfo);

		return "/PersonManSystemHtml/index/RLDJ.html";
	}
}
